/**
 * 通用数据效验
 */

/**
 * 效验电话号码
 */
export const telRule = (options) => {
    return Object.assign({
        required: true,
        message: '请输入电话号码！',
        trigger: ['blur','change'],
        validator: (rule, value, callback) => {
            const tempVal = ''+value;
            if (tempVal && tempVal.trim() == '') {
                callback(new Error('请输入电话号码'))
            }
            // else if (isNaN(value)) {
            //     callback(new Error('请输入电话号码'))
            // }
            else if (rule.required == true && !tempVal) {
                callback(new Error('请输入电话号码'))
            } else {
                callback()
            }
        }
    }, options || {})
}

/**
 * 手机号效验
 */
export const phoneRule = (options) => {
    return Object.assign({
        required: true,
        message: '请输入正确的手机号！',
        trigger: 'blur',
        validator: (rule, value, callback) => {
            const tempVal = ''+value;

            if (tempVal && tempVal.trim() == '') {
                callback(new Error('请输入正确的手机号'))
            } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(tempVal))) {
                callback(new Error('请输入正确的手机号'))
            } else {
                callback()
            }
        }
    }, options || {})
}

/**
 * 非空效验
 */
export const notEmptyRule = (options) => {
    return Object.assign({
        required: true,
        message: '请输入内容！',
        trigger: ['blur','change'],
        validator: (rule, value, callback) => {
            if(value==0){
                callback();
                return;
            }
            if (value && value.toString().trim() != '') {
                callback();
            } else {
                callback(new Error(''));
            }
        }
    }, options || {});
}

/**
 * 长度效验
 */
 export const lenRule = (options) => {
    return Object.assign({
        required: false,
        message: `内容最大不超过${options.max}字`,
        trigger: 'change',
        validator: (rule, value, callback) => {
            if (value&&value.toString().length>options.max) {
                callback(new Error(''));
            } else {
                callback()
            }
        }
    }, options || {});
}